#include "CommunicationsHandler.h"

uint8 u8SMAC_TxBuffer[SMAC_TX_BUFFER_SIZE];

uint8 u8SMAC_RxBuffer[SMAC_RX_BUFFER_SIZE];

uint8 u8IIC_NumToRec;
uint8 u8IIC_AddrCycle;
uint8 u8IIC_TxCount;

uint8 u8IIC_TxPacket;

extern uint16 u16gps_distance;
extern uint16 u16gps_altitude;

extern uint8 u8gps_course;
extern uint8 u8gps_direction;
extern uint8 u8gps_speed;
extern uint8 u8gps_fix;
extern uint8 u8gps_satellites;
extern uint8 u8ultrasound_altitude;

extern uint8 u8battery;
extern uint8 u8airplane_direction;
extern uint8 u8ambient_temp;

extern uint8 u8airplane_status;
extern uint8 u8config_feedback1;
extern uint8 u8config_feedback2;
extern uint8 u8config_feedback3;


extern uint8 u8GPS_RxBuffer[GPS_RX_BUFFER_SIZE];

extern uint8 u8motor_targetspeed;

extern uint8 u8aileron_position;
extern uint8 u8rudder_position;
extern uint8 u8elevator_position;

extern uint8 u8directions;

extern uint8 u8autopilot_status;

uint8 u8config1 = 0;
uint8 u8config2 = 0;
uint8 u8surface_config = 0;

uint8 u8general_config = 0;

uint8 u8Comm_RequestSCIPacket (void)
{
    uint8 u8watchdog;
    uint8 u8counter;
    uint8 u8checksum_qe = 0;
    uint8 u8checksum_mc1321x = 0;
    
    (void)u8SCI2_RxPending();
    (void)u8SCI2_ReadByte();
    
    vfnSCI2_SendByte (COMM_REQUEST_PACKET);
    while(!u8SCI2_TxBufferEmpty());
    
    u8watchdog = u8RTC_ReadCounter();
    
    while(!u8SCI2_RxPending() && (u8watchdog != COMM_TIMEOUT_VALUE)) 
    {
        u8watchdog = u8RTC_ReadCounter();
    }
     
    for (u8counter = 0; u8counter < SMAC_RX_BUFFER_SIZE; u8counter++)
    {
        // Packet request timed out, transceiver is busy or disconnected
        // return error
        if (u8watchdog == COMM_TIMEOUT_VALUE)
        {
            return 0;
        }
       
        u8SMAC_RxBuffer[u8counter] = u8SCI2_ReadByte ();
        u8checksum_qe ^= u8SMAC_RxBuffer[u8counter];
        
        // Wait for next byte to be received or timeout to happen
        while(!u8SCI2_RxPending() && (u8watchdog != COMM_TIMEOUT_VALUE))
        {
            u8watchdog = u8RTC_ReadCounter();
        }
               
    }
    
    // Read checksum sent by MC1321X
    u8checksum_mc1321x = u8SCI2_ReadByte();
    
    // Send checksum calculated by QE to ensure both modules can determine
    // if transmission was succesfull or not.
    vfnSCI2_SendByte(u8checksum_qe);
    while(!u8SCI2_TxBufferEmpty());
    
    // If the checksums don't match, return error
    if (u8checksum_mc1321x != u8checksum_qe)
    {
    	return 0;
    }
    
    // If reception was correct, return success
    return 1; 
}
    

void vfnComm_ReadSCIPacket (void)
{
    u8motor_targetspeed = u8SMAC_RxBuffer[0];
    
    u8aileron_position = u8SMAC_RxBuffer[1];
    
    u8rudder_position = u8SMAC_RxBuffer[2];
    
    u8elevator_position = u8SMAC_RxBuffer[3];
    
    u8config1 = u8SMAC_RxBuffer[4];
    
    u8config2 = u8SMAC_RxBuffer[5];
    
    u8surface_config = u8SMAC_RxBuffer[6];
    
    u8autopilot_status = u8SMAC_RxBuffer[7];
    
    if ((u8surface_config & 0xC0) != 0xC0)
    {
    	u8surface_config = 0;
    }
    
    if ((u8config1 & 0xF0) == 0xF0)
    {
        u8general_config = ((u8config1 & 0x0F) << 4);
    }
    else
    {
        u8general_config &= 0x0F;
    }
    
    if ((u8config2 & 0xF0) == 0xF0)
    {
        u8general_config |= (u8config2 & 0x0F);
    }
    else
    {
        u8general_config &= 0xF0;
    }

	

}

void vfnComm_BuildSCIPacket (void)
{
    uint16 u16tempo;
    u8SMAC_TxBuffer[0] = (uint8)(u16gps_distance >> 8);
    u8SMAC_TxBuffer[1] = (uint8)(u16gps_distance & 0x00FF);
    
    //u8SMAC_TxBuffer[0] =
    //u8SMAC_TxBuffer[1] = 
    
    u8SMAC_TxBuffer[2] = u8directions;
    
    u8SMAC_TxBuffer[3] = u8gps_speed;
    
    u16tempo = u16gps_altitude/10;
    u8SMAC_TxBuffer[4] = (uint8)(u16tempo >> 8);
    u8SMAC_TxBuffer[5] = (uint8)(u16tempo & 0x00FF);
    
    u8SMAC_TxBuffer[6] = u8battery;
    
    u8SMAC_TxBuffer[7] = u8ambient_temp;

    u8SMAC_TxBuffer[8] = u8airplane_status;
        
    u8SMAC_TxBuffer[9] = u8config_feedback1;
    
    u8SMAC_TxBuffer[10] = u8config_feedback2;
    
    u8SMAC_TxBuffer[11] = u8config_feedback3;
        
    u8config_feedback1 = 0;
    u8config_feedback2 = 0;
    u8config_feedback3 = 0; 
            
}

uint8 u8Comm_SendSCIPacket (void)
{
    uint8 u8counter;
    uint8 u8checksum = 0;
    uint8 u8watchdog;   
 
    (void)u8SCI2_RxPending();
    (void)u8SCI2_ReadByte();
 
    // Send the header identifying packet 1
    vfnSCI2_SendByte (COMM_SCI_PACKET_HEADER);
    while(!u8SCI2_TxBufferEmpty());
    
    // Send the corresponding buffer while calculating checksum  
    for(u8counter = 0; u8counter < SMAC_TX_BUFFER_SIZE; u8counter++)
    {
        vfnSCI2_SendByte (u8SMAC_TxBuffer[u8counter]);
        u8checksum ^= u8SMAC_TxBuffer[u8counter];
        while(!u8SCI2_TxBufferEmpty());          
    }
    
    // Send the checksum
    vfnSCI2_SendByte (u8checksum);
    while(!u8SCI2_TxBufferEmpty());
    
    u8watchdog = u8RTC_ReadCounter();
    while(!u8SCI2_RxPending() && (u8watchdog != COMM_TIMEOUT_VALUE))
    {
        u8watchdog = u8RTC_ReadCounter();
    }
    
    if (u8watchdog == COMM_TIMEOUT_VALUE)
    {
        return 0;
    }
    else
    {
        if (u8checksum == u8SCI2_ReadByte())
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
}

void vfnComm_ConfigureSCI (void)
{
    vfnSCI_SetBaudRate (COMM_TRANSCEIVER_SCI_CHAN, SCI_62500BAUD);
    vfnSCI_Init (COMM_TRANSCEIVER_SCI_CHAN);            
}

void vfnComm_ConfigureIIC (void)
{
    // Initialize the corresponding IIC module
    
    vfnIIC_CallAddressStatus (COMM_GPS_IIC_CHAN, IIC_CALL_ADDRESS_ENABLED);
    vfnIIC_AddressMode (COMM_GPS_IIC_CHAN, IIC_ADDRESS_MODE_7BITS);
    vfnIIC_SetAddress (COMM_GPS_IIC_CHAN, COMM_IIC_CALLBACK_ADDR);
    vfnIIC_BaudRate (COMM_GPS_IIC_CHAN, IIC_BAUDRATE_100KBPS);
    vfnIIC_ModuleStatus (COMM_GPS_IIC_CHAN, IIC_MODULE_ENABLED);

}

uint8 u8Comm_IICReadPacket (void)
{
    uint8 u8counter;
    uint8 u8checksum = 0;
    
    for(u8counter = 0; u8counter < GPS_RX_BUFFER_SIZE - 1; u8counter++)
    {
        u8checksum ^= u8GPS_RxBuffer[u8counter];
    }

    if (u8checksum == u8GPS_RxBuffer[GPS_RX_BUFFER_SIZE - 1])
    {
        u16gps_distance = u8GPS_RxBuffer[0];
        u16gps_distance *= 10;
        u16gps_distance += u8GPS_RxBuffer[1];
        
        u16gps_altitude = u8GPS_RxBuffer[2];
        u16gps_altitude *= 10;
        u16gps_altitude += u8GPS_RxBuffer[3];
        
        u8gps_course = u8GPS_RxBuffer[4];
        
        u8gps_direction = u8GPS_RxBuffer[5];
        
        u8gps_speed = u8GPS_RxBuffer[6];
        
        u8gps_fix = u8GPS_RxBuffer[7];
        
        if( (u8gps_fix > 0) && (u8gps_fix < 4) )
        {
        	u8airplane_status |= STATUS_GPS_FIX_READY; 
        }
        else
        {
        	u8airplane_status &= ~STATUS_GPS_FIX_READY; 	
        }
        
        u8gps_satellites = u8GPS_RxBuffer[8];
        
        u8ultrasound_altitude = u8GPS_RxBuffer[9];
        
        u8directions = 0;
        
        if(u8gps_direction > 101)
        {
            if(u8gps_direction > 168)
            {
                 u8directions |= DIR_POS_N;
            }
            else if(u8gps_direction > 146)
            {
                 u8directions |= ( DIR_POS_N | DIR_POS_W );
            }
            else if(u8gps_direction > 124)
            {
                 u8directions |= DIR_POS_W;
            }
            else
            {
                 u8directions |= ( DIR_POS_S | DIR_POS_W );
            }
        }
        else if(u8gps_direction > 56)
        {
            if(u8gps_direction > 79)
            {
                 u8directions |= DIR_POS_S;
            }
            else
            {
                 u8directions |= ( DIR_POS_S | DIR_POS_E );
            }
        }
        else
        {
            if(u8gps_direction > 34)
            {
                 u8directions |= DIR_POS_E;
            }
            else if(u8gps_direction > 11)
            {
                 u8directions |= ( DIR_POS_E | DIR_POS_N );
            }
            else
            {
                 u8directions |= DIR_POS_N;
            }
        }
        
        if(u8gps_course > 101)
        {
            if(u8gps_course > 168)
            {
                 u8directions |= DIR_MOVEMENT_N;
            }
            else if(u8gps_course > 146)
            {
                 u8directions |= ( DIR_MOVEMENT_N | DIR_MOVEMENT_W );
            }
            else if(u8gps_course > 124)
            {
                 u8directions |= DIR_MOVEMENT_W;
            }
            else
            {
                 u8directions |= ( DIR_MOVEMENT_S | DIR_MOVEMENT_W );
            }
        }
        else if(u8gps_course > 56)
        {
            if(u8gps_course > 79)
            {
                 u8directions |= DIR_MOVEMENT_S;
            }
            else
            {
                 u8directions |= ( DIR_MOVEMENT_S | DIR_MOVEMENT_E );
            }
        }
        else
        {
            if(u8gps_course > 34)
            {
                 u8directions |= DIR_MOVEMENT_E;
            }
            else if(u8gps_course > 11)
            {
                 u8directions |= ( DIR_MOVEMENT_E | DIR_MOVEMENT_N );
            }
            else
            {
                 u8directions |= DIR_MOVEMENT_N;
            }
        }
        
        return 1;
    }
    
    return 0;
           
}

void vfnIIC_MasterTransmit (uint8 u8addr, uint8 u8data)
{ 
    // Set TX bit for Address cycle  
    vfnIIC_DataDirection (COMM_GPS_IIC_CHAN, IIC_TRANSMIT);
    
    // Set Master Bit to generate a Start
    vfnIIC_ModeSelect (COMM_GPS_IIC_CHAN, IIC_MODE_MASTER);
    
    // Send Address data LSB is R or W for Slave
    vfnIIC_SendData (COMM_GPS_IIC_CHAN, u8addr);
    
    u8IIC_TxCount = 0;
    
    u8IIC_TxPacket = u8data; 
    
    while (!u8IIC_TransferComplete(COMM_GPS_IIC_CHAN));            
}


void vfnIIC_ServiceModule (void)             
{                                                                             

    vfnIIC_ClearInterruptFlag(COMM_GPS_IIC_CHAN);  // Clear Interrupt Flag
    
    if (u8IIC_MasterMode(COMM_GPS_IIC_CHAN))       // Master or Slave?
    {
    
    /***************************** Master **********************************/
        
        // Transmit or Receive?
        if (u8IIC_TransmitMode(COMM_GPS_IIC_CHAN))                        
        {                         
            
    /**************************** Transmit *********************************/
            
            // Is the Last Byte? 
            if (u8IIC_TxCount == GPS_TX_BUFFER_SIZE)                                     
            {
                // Generate a Stop                
                vfnIIC_ModeSelect(COMM_GPS_IIC_CHAN, IIC_MODE_SLAVE);      
            }
            else 
            {
                // Check for ACK
                if (u8IIC_AckReceived(COMM_GPS_IIC_CHAN))                 
                {
                    // No ACk Generate a Stop                                                        
                    vfnIIC_ModeSelect(COMM_GPS_IIC_CHAN, IIC_MODE_SLAVE);  
                }                                                     
                else  // ACk received.                                               
                {
                    // Is Address Cycle finished?
                    if (u8IIC_AddrCycle == 1)                             
                    {
                        // Master done addressing Slave? 
                        // Clear Addr cycle                                                
                        u8IIC_AddrCycle = 0;
                        
                        // Switch to RX mode                              
                        vfnIIC_DataDirection (COMM_GPS_IIC_CHAN,IIC_RECEIVE);
                        
                        // Dummy read from Data Register
                        (void)u8IIC_ReadData (COMM_GPS_IIC_CHAN);            
                    }
                    else 
                    {
                        // Transmit Data
                        vfnIIC_SendData (COMM_GPS_IIC_CHAN, u8IIC_TxPacket);         
                        u8IIC_TxCount++;
    
                    }
                }
            }
        }
 
        else 
        {
        
   /************************************ Receive ****************************************/
           
            if (u8IIC_NumToRec == 1)        // Last byte to be read?
            {
                // Generate a Stop
                vfnIIC_ModeSelect(COMM_GPS_IIC_CHAN,IIC_MODE_SLAVE);                      
                vfnIIC_Master_Read_and_Store();
            }  
            else if (u8IIC_NumToRec == 2)     // Second to last byte to be read?
            { 
                // This sets up a NACK
                vfnIIC_TxAcknowledgeStatus(COMM_GPS_IIC_CHAN,IIC_TX_ACK_DISABLED);   
                vfnIIC_Master_Read_and_Store();
            }  
            else 
            {
                vfnIIC_Master_Read_and_Store();
            }
        }
    }
 
    else 
    {
    }
}

void vfnIIC_Master_Read_and_Store(void) 
{
    u8GPS_RxBuffer[GPS_RX_BUFFER_SIZE - u8IIC_NumToRec] = u8IIC_ReadData(COMM_GPS_IIC_CHAN);
    u8IIC_NumToRec--;
}


void vfnIIC_MasterReceive (uint8 u8slave) 
{
    u8IIC_TxCount = 0;
    u8IIC_AddrCycle = 1;
    u8IIC_NumToRec = GPS_RX_BUFFER_SIZE;

    vfnIIC_TxAcknowledgeStatus(COMM_GPS_IIC_CHAN, IIC_TX_ACK_ENABLED);
    vfnIIC_ClearInterruptFlag(COMM_GPS_IIC_CHAN);
    
    // Set TX bit for Address cycle
    vfnIIC_DataDirection(COMM_GPS_IIC_CHAN,IIC_TRANSMIT);
    
    // Set Master Bit to generate a Start   
    vfnIIC_ModeSelect(COMM_GPS_IIC_CHAN,IIC_MODE_MASTER);
    
    // Send Address data LSB is R or W for Slave  
    vfnIIC_SendData(COMM_GPS_IIC_CHAN, (u8slave | 0x01));         
    
    while (!u8IIC_TransferComplete(COMM_GPS_IIC_CHAN));
}


/* For speed purposes, this functions bypass the layered architecture*/
#pragma INLINE
void vfnVSCI_SendByte (uint8 u8byte)
{
	uint8 u8counter;

	// Clear timer flag just in case
	(void)TPM2C1SC_CH1F;
	TPM2C1SC_CH1F = 0;
		
	// Start bit	
	VSCI_TX_PIN = 0;
	TPM2C1V = TPM2CNT + VSCI_TIMER_DELAY_START_BIT;
	
	while(!TPM2C1SC_CH1F);
	TPM2C1SC_CH1F = 0;	// CLEAR FLAG
	
	u8counter = 8;
	
	while(u8counter)
	{
		if (u8byte & 0x01)
		{
			VSCI_TX_PIN = 1;
		}
		else
		{
			VSCI_TX_PIN = 0;
		}
		
		TPM2C1V += VSCI_TIMER_DELAY_VALUE;
		u8byte >>= 1;
		u8counter--;
		
		while(!TPM2C1SC_CH1F);
		TPM2C1SC_CH1F = 0;	// CLEAR FLAG
	}
	
	VSCI_TX_PIN = 1;	  // stop bit
	TPM2C1V += VSCI_TIMER_DELAY_VALUE;
	
	while(!TPM2C1SC_CH1F); 
	TPM2C1SC_CH1F = 0;	// CLEAR FLAG

}

#pragma INLINE
uint8 u8VSCI_ReadByte (void)
{
	uint8 u8temp_buffer;
	uint8 u8counter;
	
	// Clear flag just in case
	(void)TPM2C1SC_CH1F;
	TPM2C1SC_CH1F = 0;
	
	TPM2C1V = TPM2CNT + VSCI_TIMER_HALF_DELAY;
	
	while(!TPM2C1SC_CH1F);
	TPM2C1SC_CH1F = 0;	// CLEAR FLAG
	
	if(VSCI_RX_PIN != 0)
	{
		return 0;
	}
	u8counter = 8;
	
	while(u8counter)
	{
		TPM2C1V += VSCI_TIMER_DELAY_VALUE;
		
		while(!TPM2C1SC_CH1F);
		TPM2C1SC_CH1F = 0;	// CLEAR FLAG
		
		u8temp_buffer >>= 1;
		if(VSCI_RX_PIN)
		{
			u8temp_buffer |= 0x80;
		}
		u8counter--;	
	}
	
	TPM2C1V += VSCI_TIMER_DELAY_VALUE;
	
	while(!TPM2C1SC_CH1F);
	TPM2C1SC_CH1F = 0;	// CLEAR FLAG
	
	if (VSCI_RX_PIN != 1)
	{
		return 0;
	}

	return u8temp_buffer;
}

void vfnVSCI_Configure (void)
{
	VSCI_TX_PIN = 1;
	
	VSCI_TX_PIN_DD = GPIO_OUTPUT;
	VSCI_RX_PIN_DD = GPIO_INPUT;

	vfnTPM_SetModulo (VSCI_TIMER, 0x0000);
	
	vfnTPM_PrescalerSelect (VSCI_TIMER, TPM_PRESCALER_2);
	
	vfnTPM2_SoftwareOutputCompare (VSCI_TIMER_CHANNEL);
	
	vfnTPM_ClockSelect (VSCI_TIMER, TPM_BUS_CLOCK);
			
}

#pragma MESSAGE DISABLE C4301 // inline expansion for function call
/*************		FALTA PONERLE TIMEOUT	 ***************/
uint8 u8Comm_RequestGPSPacket (void)	  
{
	uint8 u8loop_counter;
	uint8 u8checksum = 0;
	
	vfnVSCI_SendByte (GPS_PACKET_REQUEST);
	
	for (u8loop_counter = 0; u8loop_counter < GPS_RX_BUFFER_SIZE - 1; u8loop_counter++)
	{
		while(VSCI_RX_PIN && (RTCCNT < 120));
		
		if(RTCCNT > 119)
		{
			return 0;
		}
		
		u8GPS_RxBuffer[u8loop_counter] = u8VSCI_ReadByte();
		u8checksum ^= u8GPS_RxBuffer[u8loop_counter];
	}
	
	while(VSCI_RX_PIN && (RTCCNT < 120));
	
	if(RTCCNT > 119)
	{
		return 0;
	}
	
    u8GPS_RxBuffer[GPS_RX_BUFFER_SIZE - 1] = u8VSCI_ReadByte();		
	
	vfnVSCI_SendByte (u8checksum);
		
	if (u8checksum == u8GPS_RxBuffer[GPS_RX_BUFFER_SIZE - 1])
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

/*************		FALTA PONERLE TIMEOUT	 ***************/
uint8 u8Comm_SendGPSCommand (uint8 u8command)
{
	uint8 u8feedback;
	
	vfnVSCI_SendByte (u8command);
	
	while(VSCI_RX_PIN && (RTCCNT < 120));
	
	if (RTCCNT > 119)
	{
		return 0;
	}
	
	u8feedback = u8VSCI_ReadByte();
	
	if (u8feedback == GPS_HOME_FEEDBACK)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void vfnComm_VSCIReadPacket (void)
{
    u16gps_distance = u8GPS_RxBuffer[0];
    u16gps_distance <<= 8;
    u16gps_distance += u8GPS_RxBuffer[1];
    
    u16gps_altitude = u8GPS_RxBuffer[2];
    u16gps_altitude <<= 8;
    u16gps_altitude += u8GPS_RxBuffer[3];
    
    u8gps_course = u8GPS_RxBuffer[4];
    
    u8gps_direction = u8GPS_RxBuffer[5];
    
    u8gps_speed = u8GPS_RxBuffer[6];
    
    u8gps_fix = u8GPS_RxBuffer[7];
    
    if( (u8gps_fix > 0) && (u8gps_fix < 4) )
    {
    	u8airplane_status |= STATUS_GPS_FIX_READY; 
    }
    else
    {
    	u8airplane_status &= ~STATUS_GPS_FIX_READY; 	
    }
    
    u8gps_satellites = u8GPS_RxBuffer[8];
    
    u8ultrasound_altitude = u8GPS_RxBuffer[9];
    
    u8directions = 0;
    
    if(u8gps_direction > 101)
    {
        if(u8gps_direction > 168)
        {
             u8directions |= DIR_POS_N;
        }
        else if(u8gps_direction > 146)
        {
             u8directions |= ( DIR_POS_N | DIR_POS_W );
        }
        else if(u8gps_direction > 124)
        {
             u8directions |= DIR_POS_W;
        }
        else
        {
             u8directions |= ( DIR_POS_S | DIR_POS_W );
        }
    }
    else if(u8gps_direction > 56)
    {
        if(u8gps_direction > 79)
        {
             u8directions |= DIR_POS_S;
        }
        else
        {
             u8directions |= ( DIR_POS_S | DIR_POS_E );
        }
    }
    else
    {
        if(u8gps_direction > 34)
        {
             u8directions |= DIR_POS_E;
        }
        else if(u8gps_direction > 11)
        {
             u8directions |= ( DIR_POS_E | DIR_POS_N );
        }
        else
        {
             u8directions |= DIR_POS_N;
        }
    }
    
    if(u8gps_course > 101)
    {
        if(u8gps_course > 168)
        {
             u8directions |= DIR_MOVEMENT_N;
        }
        else if(u8gps_course > 146)
        {
             u8directions |= ( DIR_MOVEMENT_N | DIR_MOVEMENT_W );
        }
        else if(u8gps_course > 124)
        {
             u8directions |= DIR_MOVEMENT_W;
        }
        else
        {
             u8directions |= ( DIR_MOVEMENT_S | DIR_MOVEMENT_W );
        }
    }
    else if(u8gps_course > 56)
    {
        if(u8gps_course > 79)
        {
             u8directions |= DIR_MOVEMENT_S;
        }
        else
        {
             u8directions |= ( DIR_MOVEMENT_S | DIR_MOVEMENT_E );
        }
    }
    else
    {
        if(u8gps_course > 34)
        {
             u8directions |= DIR_MOVEMENT_E;
        }
        else if(u8gps_course > 11)
        {
             u8directions |= ( DIR_MOVEMENT_E | DIR_MOVEMENT_N );
        }
        else
        {
             u8directions |= DIR_MOVEMENT_N;
        }
    }
        
}


/******************************************************************************

	DEVOLVER ESTAS FUNCIONES CUANDO IIC SIRVA BIEN

uint8 u8Comm_RequestGPSPacket (void)
{
	uint8 u8watchdog;
    vfnIIC_MasterTransmit (COMM_GPS_MANAGER_IIC_ADDRESS, GPS_PACKET_REQUEST);
    
    while (!u8IIC_TransferComplete(COMM_GPS_IIC_CHAN));
    
    if(!u8IIC_AckReceived  (COMM_GPS_IIC_CHAN))     //Acknowledge Received??
    {                                               //YES!
        while(u8IIC_MasterMode (COMM_GPS_IIC_CHAN))
        {    
            while(!u8IIC_CheckInterrupt (COMM_GPS_IIC_CHAN));
        
            vfnIIC_ServiceModule();        
        }

        vfnIIC_MasterReceive (COMM_GPS_MANAGER_IIC_ADDRESS);
        
        while(u8IIC_MasterMode (COMM_GPS_IIC_CHAN))
        {    
            u8watchdog = u8RTC_ReadCounter();
            
            while((!u8IIC_CheckInterrupt (COMM_GPS_IIC_CHAN)) && (u8watchdog != COMM_TIMEOUT_VALUE))
            {
                u8watchdog = u8RTC_ReadCounter();
            }
            
            if(u8watchdog == COMM_TIMEOUT_VALUE) 
            {
            	vfnIIC_ModeSelect (COMM_GPS_IIC_CHAN,IIC_MODE_SLAVE);
            }
            
            vfnIIC_ServiceModule();        
        }

    } 
    else 
    {
        // No acknowledge received
        
        return 0;
    }
    
    return 1;
}

uint8 u8Comm_SendGPSCommand (uint8 u8command)
{
    vfnIIC_MasterTransmit (COMM_GPS_MANAGER_IIC_ADDRESS, u8command);
    
    if(!u8IIC_AckReceived  (COMM_GPS_IIC_CHAN))     //Acknowledge Received??
    {                                               //YES!
        while(u8IIC_MasterMode (COMM_GPS_IIC_CHAN))
        {    
            //u8timeout = 0;
            while(!u8IIC_CheckInterrupt (COMM_GPS_IIC_CHAN))
            {
                //u8timeout++; 
                //if(u8timeout == 255) 
                //{
                //    vfnIIC_ModeSelect (COMM_GPS_IIC_CHAN, IIC_MODE_SLAVE);
                //}
            }
        
            vfnIIC_ServiceModule();        
        }
    } 
    else 
    {
        // No acknowledge received
        
        return 0;
    }
    
    // Command sent correctly
    
    return 1;
       
}

******************************************************************************/


